<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="客户名称/直播间名称：" prop="keyword">
          <el-input v-model="queryParams.keyword" placeholder="请输入" clearable style="width: 260px;" />
        </el-form-item>
        <el-form-item label="客户负责人：" prop="responsiblePerson">
          <el-select v-model="queryParams.responsiblePerson" placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in responsiblePersonList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="直播渠道：" prop="liveChannelType">
          <el-select v-model="queryParams.liveChannelType" placeholder="请选择" style="width: 240px;">
            <el-option v-for="item in statusList" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="直播时间：">
          <el-date-picker clearable v-model="date" type="daterange" format="yyyy-MM-dd"
            value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
            @change="changeTime" placeholder="请选择">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <div></div>
        <export-excel-button style="margin: 0 0 20px" title="直播数据总汇"
          action="/client/service/live/statistics/list/export" type="2" :params="queryParams"  v-hasAuth="'client:service:live:statistics:export'"/>
      </div>
      <el-table v-loading="loading" ref="multipleTable" :data="tabList" stripe border tooltip-effect="dark"
        style="width: 100%" show-summary :summary-method="getSummaries">
        <el-table-column label="直播基础数据" align="center">
          <el-table-column label="序号" align="center" type="index" width="64px" />
          <el-table-column label="客户名称" align="center" prop="clientName"></el-table-column>
          <el-table-column label="客户负责人" align="center" prop="responsiblePerson"></el-table-column>
          <el-table-column label="直播日期" align="center" prop="liveDay"></el-table-column>
          <el-table-column label="直播间名称" align="center" prop="liveName"></el-table-column>
          <el-table-column label="开播时间" align="center" prop="liveStartTime"></el-table-column>
          <el-table-column label="直播时长" align="center" prop="liveHour"></el-table-column>
          <el-table-column label="直播渠道" align="center" prop="liveChannelType"></el-table-column>
          <el-table-column label="直播预约人数" align="center" prop="liveAppointmentNumber"></el-table-column>
          <el-table-column label="宣传投流成本（元）" align="center" prop="flowCost"></el-table-column>
          <el-table-column label="直播间曝光人数（投流/公域流量曝光人数）" align="center" prop="liveExposureNumber"></el-table-column>
          <el-table-column label="直播间观看人数" align="center" prop="liveWatchNumber"></el-table-column>
          <el-table-column label="商品曝光人数" align="center" prop="goodsExposureNumber"></el-table-column>
          <el-table-column label="商品点击人数" align="center" prop="goodsClickNumber"></el-table-column>
          <!-- <el-table-column label="直播时段注册人数" align="center" prop="liveRegisterNumber"></el-table-column> -->
          <el-table-column label="成交订单数" align="center" prop="orderNumber"></el-table-column>
          <el-table-column label="成交人数" align="center" prop="orderPeopleNumber"></el-table-column>
          <el-table-column label="销售额（元）" align="center" prop="orderSellMoney"></el-table-column>
        </el-table-column>
        <el-table-column label="直播转化情况" align="center">
          <el-table-column label="ROI" align="center" prop="roi">
            <template slot-scope="{}" slot="header">
              <span>ROI</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>销售额除以宣传投流成本</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="直播间曝光/投流-进入直播间" align="center" prop="liveExposureRate">
            <template slot-scope="{}" slot="header">
              <span>直播间曝光/投流-进入直播间</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>直播间观看人数除以直播间曝光人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="直播间进入-商品曝光率" align="center" prop="goodsExposureRate">
            <template slot-scope="{}" slot="header">
              <span>直播间进入-商品曝光率</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>商品曝光人数除以直播间观看人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="商品曝光-商品点击率" align="center" prop="goodsClickRate">
            <template slot-scope="{}" slot="header">
              <span>商品曝光-商品点击率</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>商品点击人数除以商品曝光人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="商品点击-成交人数" align="center" prop="goodsOrderRate">
            <template slot-scope="{}" slot="header">
              <span>商品点击-成交人数</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>成交订单数除以商品点击人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="观看人数-成交转化率" align="center" prop="liveOrderRate">
            <template slot-scope="{}" slot="header">
              <span>观看人数-成交转化率</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>成交人数除以直播间观看人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="单个用户投流-成交转化成本（元）" align="center" prop="liveOrderCost">
            <template slot-scope="{}" slot="header">
              <span>单个用户投流-成交转化成本（元）</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>宣传投流成本除以成交人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="人均下单数" align="center" prop="personAverageOrderNum">
            <template slot-scope="{}" slot="header">
              <span>人均下单数</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>成交订单数除以成交人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column label="人均消费金额（元）" align="center" prop="personAverageMoney">
            <template slot-scope="{}" slot="header">
              <span>人均消费金额（元）</span>
              <el-tooltip class="item" effect="dark" placement="top">
                <i class="el-icon-question tip_icon"></i>
                <div slot="content">
                  <p>销售额除以成交人数</p>
                </div>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" :page-sizes="[50, 100, 300, 500, 1000]"></pagination>
    </div>

    <!-- 直播结果弹窗 -->
    <LiveResultDialog v-if="dialogVisible" :id="dialogId" :pathType="pathType" :visible.sync="dialogVisible"
      @success="_handleSuccess">
    </LiveResultDialog>
  </div>
</template>

<script>
import {
  statisticsList,
} from "@/api/customer/serviceData";
import {
  responsibleSelect,
} from "@/api/customer/customerFile";
import LiveResultDialog from "./components/liveResultDialog";
import ExportExcelButton from "@/components/ExportExcelButton/index";
export default {
  name: "",
  components: {
    LiveResultDialog,
    ExportExcelButton
  },
  data() {
    return {
      dialogId: '',
      dialogVisible: false,
      watchVisible: false,
      // 遮罩层
      loading: false,
      // 导出遮罩层
      // 总条数
      total: 0,
      //表格数据
      tabList: [],
      pathType: 'add',
      date: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        beginTime: '',
        endTime: '',
        keyword: '',
        liveChannelType: '',
        responsiblePerson: '',
      },
      shopType: "",
      responsiblePersonList: [],
      statusList: [
        { id: '', value: '全部' },
      ],
    };
  },
  computed: {

  },
  created() {
    this.getDict()
    this.getresponsiblePersonList()
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      statisticsList(this.queryParams).then(response => {
        response.result.records.forEach(item => {
          if (item.roi <= 0) {
            item.roi = '-'
          }
        })
        this.tabList = response.result.records;

        this.total = response.result.total;
        this.loading = false;

      });
    },
    //客户负责人下拉
    getresponsiblePersonList() {
      responsibleSelect().then((res) => {
        this.responsiblePersonList = res.result
      })
        .finally(() => {
          this.loading = false;
        });
    },

    //获取状态字典
    getDict() {
      var dictType = "live_channel_type";
      this.$getSelectList(dictType)
        .then((res) => {
          this.statusList = this.statusList.concat(res.result[0].dictList)
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //处理时间
    changeTime(e) {
      this.queryParams.beginTime = e[0]
      this.queryParams.endTime = e[1]
    },
    // 表单重置
    reset() {
      this.date = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        beginTime: '',
        endTime: '',
        keyword: '',
        liveChannelType: '',
        responsiblePerson: '',
      };
      this.resetForm("queryParams");
      this.handleQuery()
    },
    //编辑
    editClick(row) {
      this.dialogId = row.liveId
      this.dialogVisible = true;
      this.pathType = 'add'
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.reset();
    },
    _handleSuccess(e) {
      //成功回调
      this.resetQuery()
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '小计';
          return;
        }
        //column.property == 'liveHour'只要换上你想汇总列的prop属性值就可以实现某一行汇总
        if (column.property == 'liveHour') {
          const values = data.map((item) => String(item[column.property]));
          if (values.length > 0) {
            const total = values.reduce((prev, curr) => {
              const value = curr;
              if (value != '') {
                const currs = curr.split(':');
                const currsH = currs[0] * 3600;
                const currsM = currs[1] * 60;
                const currsS = currs[2] * 1;
                return this.$cal.accAdd(parseFloat(prev),parseFloat(currsH + currsM + currsS));
              } else {
                return prev;
              }
            }, 0);
            const S = total % 60
            const M = parseInt(total % 3600 / 60);
            const H = parseInt(total / 3600)
            sums[index] = H + ':' + M + ":" + S
          }
        } else if (column.property == 'liveAppointmentNumber' ||
          column.property == 'flowCost' ||
          column.property == 'liveExposureNumber' ||
          column.property == 'liveWatchNumber' ||
          column.property == 'goodsExposureNumber' ||
          column.property == 'goodsClickNumber' ||
          column.property == 'liveRegisterNumber' ||
          column.property == 'orderNumber' ||
          column.property == 'orderPeopleNumber' ||
          column.property == 'orderSellMoney' ||
          column.property == 'roi' ||
          column.property == 'liveOrderCost' ||
          column.property == 'personAverageOrderNum' ||
          column.property == 'personAverageMoney') {
          const values = data.map((item) => Number(item[column.property]));
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return this.$cal.accAdd(prev, curr);
              } else {
                return prev;
              }
            }, 0);
          }
        } else if (column.property == 'liveExposureRate' ||
          column.property == 'goodsExposureRate' ||
          column.property == 'goodsClickRate' ||
          column.property == 'goodsOrderRate' ||
          column.property == 'liveOrderRate') {
          const values = data.map((item) =>
            String(item[column.property]).substring(0, String(item[column.property]).length - 1)
          );
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = curr;
              if (!isNaN(value)) {
                return this.$cal.accAdd(parseFloat(prev), parseFloat(curr));
              } else {
                return prev;
              }
            }, 0) + '%';
          }
        } else {
          sums[index] = '-';
        }
      });
      return sums;
    }
  }
};

</script>
<style scoped>
.head {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px 0;
}

.listBox {
  box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
  background-color: #fff;
  border-radius: 8px;
  padding: 20px 20px;
  margin-top: 10px;
}

.listBox_head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.img_box {
  width: 100px;
  height: 80px;
  border-radius: 4px;
}

.contentTitle {
  width: 100%;
  display: flex;
}
</style>